package com.icebartech.core.components;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.icebartech.core.constants.IcebartechConstants;
import com.icebartech.core.constants.WeiXinConstant;

/**
 * 登录组件类
 * @author wenhsh
 */
@Component
public class LoginComponent {
	
	@Autowired
	private RedisComponent redisComponent;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(LoginComponent.class);
	
	/**
	 * 后台用户登录
	 * @param userCore 用户基本信息对象
	 */
	public void adminLogin(String sessionId, Long userId){
		redisComponent.putRedisCache(IcebartechConstants.ADMIN_SESSION_GROUP_KEY, sessionId, userId.toString());
		//刷新redis的session过期时间
		redisComponent.setKeyExpire(IcebartechConstants.ADMIN_SESSION_GROUP_KEY, sessionId, WeiXinConstant.SESSION_EXPIRES);
	}
	
	/**
	 * 后台用户注销登录
	 */
	public void adminLogout(String sessionId){
		redisComponent.deleteRedisCache(IcebartechConstants.ADMIN_SESSION_GROUP_KEY, sessionId);
	}
	
	/**
	 * 微信端普通用户登录，不写cookie，而是把redis里面对应的key重新设置一下时间
	 */
	public void weixinLogin(String sessionId, Long userId){
		LOGGER.info("sessionId:{}", sessionId);
		redisComponent.putRedisCache(IcebartechConstants.WEIXIN_SESSION_GROUP_KEY, sessionId, userId.toString());
		//刷新redis的session过期时间
		redisComponent.setKeyExpire(IcebartechConstants.WEIXIN_SESSION_GROUP_KEY, sessionId, WeiXinConstant.SESSION_EXPIRES);
	}
	
	/**
	 * 微信h5端主用户登出
	 * @param sessionId
	 */
	public void h5MasterLogout(String sessionId){
		redisComponent.deleteRedisCache(IcebartechConstants.H5_MASTER_SESSION_GROUP_KEY, sessionId);
	}
	
	/**
	 * 微信h5端主用户登录
	 * @param sessionId
	 * @param userId
	 */
	public void h5MasterLogin(String sessionId, Long userId){
		LOGGER.info("sessionId:{}", sessionId);
		redisComponent.putRedisCache(IcebartechConstants.H5_MASTER_SESSION_GROUP_KEY, sessionId, userId.toString());
		//刷新redis的session过期时间
		redisComponent.setKeyExpire(IcebartechConstants.H5_MASTER_SESSION_GROUP_KEY, sessionId, WeiXinConstant.SESSION_EXPIRES);
	}
	
	/**
	 * 微信h5端从用户登出
	 * @param sessionId
	 */
	public void h5SlaverLogout(String sessionId){
		redisComponent.deleteRedisCache(IcebartechConstants.H5_SLAVER_SESSION_GROUP_KEY, sessionId);
	}
	
	/**
	 * 微信h5端从用户登录
	 * @param sessionId
	 * @param userId
	 */
	public void h5SlaverLogin(String sessionId, Long userId){
		
		LOGGER.info("sessionId:{}", sessionId);
		
		redisComponent.putRedisCache(IcebartechConstants.H5_SLAVER_SESSION_GROUP_KEY, sessionId, userId.toString());
		//刷新redis的session过期时间
		redisComponent.setKeyExpire(IcebartechConstants.H5_SLAVER_SESSION_GROUP_KEY, sessionId, WeiXinConstant.SESSION_EXPIRES);
	}
	
	/**
	 * 信端普通用户注销登录清除redis里面的session信息
	 */
	public void weixinLogout(String sessionId){
		redisComponent.deleteRedisCache(IcebartechConstants.WEIXIN_SESSION_GROUP_KEY, sessionId);
	}
	
	/**
	 * app端普通用户登录，不写cookie，而是把redis里面对应的key重新设置一下时间
	 */
	public void appLogin(String sessionId, Long userId){
		redisComponent.putRedisCache(IcebartechConstants.APP_SESSION_GROUP_KEY, sessionId, userId.toString());
		//刷新redis的session过期时间
		redisComponent.setKeyExpire(IcebartechConstants.APP_SESSION_GROUP_KEY, sessionId, WeiXinConstant.SESSION_EXPIRES);
	}
	
	/**
	 * app端普通用户注销登录清除redis里面的session信息
	 */
	public void appLogout(String sessionId){
		redisComponent.deleteRedisCache(IcebartechConstants.APP_SESSION_GROUP_KEY, sessionId);
	}
	
	/**
	 * 获取客户端真实ip
	 * @param request
	 * @return
	 */
	public String getRemoteIP(HttpServletRequest request) {
		HttpServletRequest req = request;
		
		String ip = req.getHeader("X-nginx-real-ip");
		
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = req.getHeader("x-forwarded-for");  
		} 
		
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = req.getHeader("Proxy-Client-IP");  
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
			ip = req.getHeader("WL-Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip = req.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip = req.getHeader("HTTP_X_REAL_IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
			ip = req.getRemoteAddr();
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			return "";
		}

		return ip;
	}
}
